package sys.tests.automat;

import sys.fsm.*;
import sys.message.*;
import sys.transport.*;
import sys.database.*;

import sys.*;
import org.junit.* ;
import static org.junit.Assert.* ;

public class LogicTest
{
    @Test
    public void test_1()
    {
        RequestManager rm = new RequestManager("//home//chigorin//botva//term10//my//ds//cw//distsyst//sysa//src//sys//config");
        rm.start();
        mailServer = "localhost.localdomain";
        portSMTP = "10025";
        sysaAddress = "systema@localhost.localdomain"; 
        DBHost = "localhost.localdomain"; 
        DBName = "systema";
        user = "systema";
        pass = "systema";

        System.out.println("-------------------------------------------------------------");
        System.out.println("Тест №1. Удачный сценарий.");
        System.out.println("-------------------------------------------------------------");

        UniMessage mes = null; 
        try
        {
            int id = SysaDatabase.maxRequestID(DBHost, DBName, user, pass) + 1;

            // Запрос на создание заявки
            mes = new JSONMessage("systemb@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "newRequest");
            mes.put("Quantity", 10);
            mes.put("PayingSystem", "systemc@localhost.localdomain");
            mes.put("UserName", "user_1");
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(2000);
            // Подтверждение от оптового поставщика 
            mes = new JSONMessage("systemb@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "ackSeller1");
            mes.put("ackOn", id);
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(1000);
            // Подтверждение от системы оплаты 
            mes = new JSONMessage("systemc@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "ackPayingSystem");
            mes.put("ackOn", id);
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(3000);
            // Подтверждение от пользователя
            mes = new JSONMessage("user@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "ackUser");
            mes.put("ackOn", id);
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(2000);
            // Второе подтверждение от оптового поставщика
            mes = new JSONMessage("systemb@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "ackSeller2");
            mes.put("ackOn", id);
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(10000);
            assertTrue(rm.getAutomat(id).getStateId() == 6) ;
        }
        catch (Exception e)
        {
            System.out.println("Test1: Something wrong " + e.getMessage());
        }
        rm.stop();
    }

    @Test
    public void test_2()
    {
        RequestManager rm = new RequestManager("//home//chigorin//botva//term10//my//ds//cw//distsyst//sysa//src//sys//config");
        rm.start();
        mailServer = "localhost.localdomain";
        portSMTP = "10025";
        sysaAddress = "systema@localhost.localdomain"; 
        DBHost = "localhost.localdomain"; 
        DBName = "systema";
        user = "systema";
        pass = "systema";

        System.out.println("-------------------------------------------------------------");
        System.out.println("Тест №2. Оптовый поставщик не отвечает.");
        System.out.println("-------------------------------------------------------------");

        UniMessage mes = null; 
        try
        {
            int id = SysaDatabase.maxRequestID(DBHost, DBName, user, pass) + 1;

            // Запрос на создание заявки
            mes = new JSONMessage("systemb@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "newRequest");
            mes.put("Quantity", 10);
            mes.put("PayingSystem", "systemc@localhost.localdomain");
            mes.put("UserName", "user_1");
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(2000);
            // Подтверждение от оптового поставщика 
            mes = new JSONMessage("systemb@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "ackSeller1");
            mes.put("ackOn", id);
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(1000);
            // Подтверждение от системы оплаты 
            mes = new JSONMessage("systemc@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "ackPayingSystem");
            mes.put("ackOn", id);
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(3000);
            // Подтверждение от пользователя
            mes = new JSONMessage("user@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "ackUser");
            mes.put("ackOn", id);
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(10000);
            assertTrue(rm.getAutomat(id).getStateId() == 5) ;
        }
        catch (Exception e)
        {
            System.out.println("Test1: Something wrong " + e.getMessage());
        }
        rm.stop();

    } 

    @Test
    public void test_3()
    {
        RequestManager rm = new RequestManager("//home//chigorin//botva//term10//my//ds//cw//distsyst//sysa//src//sys//config");
        rm.start();
        mailServer = "localhost.localdomain";
        portSMTP = "10025";
        sysaAddress = "systema@localhost.localdomain"; 
        DBHost = "localhost.localdomain"; 
        DBName = "systema";
        user = "systema";
        pass = "systema";

        System.out.println("-------------------------------------------------------------");
        System.out.println("Тест №3. Пользователь отменяет заявку.");
        System.out.println("-------------------------------------------------------------");

        UniMessage mes = null; 
        try
        {
            int id = SysaDatabase.maxRequestID(DBHost, DBName, user, pass) + 1;

            // Запрос на создание заявки
            mes = new JSONMessage("systemb@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "newRequest");
            mes.put("Quantity", 10);
            mes.put("PayingSystem", "systemc@localhost.localdomain");
            mes.put("UserName", "user_1");
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(2000);
            // Подтверждение от оптового поставщика 
            mes = new JSONMessage("systemb@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "ackSeller1");
            mes.put("ackOn", id);
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(1000);
            // Подтверждение от системы оплаты 
            mes = new JSONMessage("systemc@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "ackPayingSystem");
            mes.put("ackOn", id);
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(3000);
            mes = new JSONMessage("user@localhost.localdomain", sysaAddress, "LOGIC TEST 1");
            mes.put("Type", "nackUser");
            mes.put("nackOn", id);
            new MessageSenderSMTP(mailServer, portSMTP).send(mes);

            Thread.sleep(10000);
            assertTrue(rm.getAutomat(id).getStateId() == 5) ;
        }
        catch (Exception e)
        {
            System.out.println("Test1: Something wrong " + e.getMessage());
        }
        rm.stop();

    } 

    private String  mailServer,
                    portSMTP,
                    sysaAddress, 
                    DBHost, 
                    DBName, 
                    user, 
                    pass;
}

